package com.qiyu.controller.backend;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qiyu.common.R;
import com.qiyu.constant.AuthRole;
import com.qiyu.entity.FirstLevelMenu;
import com.qiyu.entity.HomeBanner;
import com.qiyu.entity.HomeSetting;
import com.qiyu.model.request.backend.FirstLevelMenuReq;
import com.qiyu.model.request.backend.HomeBannerReq;
import com.qiyu.model.request.backend.HomeSettingReq;
import com.qiyu.service.FirstLevelMenuService;
import com.qiyu.service.HomeBannerService;
import com.qiyu.service.HomeSettingService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping(AuthRole.ADMIN+"/home")
@Api(tags = "首页管理")
@Slf4j
public class AdminHomeController {
    @Autowired
    private HomeSettingService homeSettingService;
    @Autowired
    private FirstLevelMenuService firstLevelMenuService;
    @Autowired
    private HomeBannerService homeBannerService;
    private String[] baseExcelude = new String[]{"deleted","createTime","updateTime"};

    @PostMapping("/homeSetting")
    @ApiOperation(value = "首页设置")
    public R<HomeSetting> homeSetting(@RequestBody HomeSettingReq req){
        return R.ok(homeSettingService.saveSetting(req),baseExcelude);
    }
    @PostMapping("/firstLevelMenuSave")
    @ApiOperation("一级菜单的增删改查")
    public R<FirstLevelMenu> firstLevelMenuSave(@RequestBody FirstLevelMenuReq req){

        return R.ok(firstLevelMenuService.saves(req),addExclude(new String[]{"menuKey"}));
    }
    @PostMapping("/firstLiveMenuOnline/{online}")
    @ApiOperation("一级菜单批量与单个上下线")
    public R firstLivelMenuOnline(@PathVariable ("online") Boolean online, Long[] ids){
        firstLevelMenuService.online(online,ids);
        return R.ok();
    }
    @GetMapping("/firstLevelMenuQuery")
    @ApiOperation(value = "一级菜单列表")
    public R<List<FirstLevelMenu>> firstLevelMenuQuery(){
        return R.ok(firstLevelMenuService.queryAll());
    }
    @GetMapping("/firstLevelMenuTop")
    @ApiOperation("一级菜单置顶操作")
    public R firstLevelMenuTop(@ApiParam(value = "top:置顶，foot:置底，up:向上，down:向下") @RequestParam String handler, @RequestParam Long id){
        firstLevelMenuService.top(handler,id);
        return R.ok();
    }
    @PostMapping("/homeBannerSave")
    @ApiOperation("轮播 增删改查")
    public R<HomeBanner> homeBannerSave(@RequestBody HomeBannerReq req){
        return R.ok(homeBannerService.saves(req),baseExcelude);

    }
    @GetMapping("/homeBannerQuery")
    @ApiOperation("轮播列表")
    public R<IPage<HomeBanner>> homeBannerQuery(Integer page, Integer size, String bannerName){
        return R.ok(homeBannerService.queryAll(page,size,bannerName));
    }
    @PostMapping("/homeBannerOnline/{online}")
    @ApiOperation("轮播批量与单个上下线")
    public R homeBannerOnline(@PathVariable ("online") Boolean online, Long[] ids){
        homeBannerService.online(online,ids);
        return R.ok();
    }
    @GetMapping("/homeBannerTop")
    @ApiOperation("轮播置顶操作")
    public R homeBannerTop(@ApiParam(value = "top:置顶，foot:置底，up:向上，down:向下") String handler, Long id){
        homeBannerService.top(handler,id);
        return R.ok();
    }

    /**
     * 添加排除字段
     * @param exlude
     * @return
     */
    private String[]  addExclude(String[] exlude){
        List<String> stringList = new ArrayList<>(Arrays.asList(baseExcelude));
        for(String s:exlude){
            stringList.add(s);
        }
        return stringList.toArray(new String[stringList.size()]);
    }

}
